//
//  XYLHHPaoPaoView.m
//  XYLBaiduMap2017
//
//  Created by 夏玉林 on 17/2/16.
//  Copyright © 2017年 夏玉林. All rights reserved.
//

#import "XYLHHPaoPaoView.h"

static CGFloat kTitleLabelFontSize = 14.0;

@interface XYLHHPaoPaoView ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *naviButton;

@end

@implementation XYLHHPaoPaoView
#pragma mark - =====================泡泡的UI====================
- (UIButton *)naviButton
{
    if (!_naviButton)
    {
        _naviButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_naviButton setBackgroundColor:[UIColor clearColor]];
        
        [_naviButton addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_naviButton];
    }
    return _naviButton;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        
        _titleLabel.textColor = [UIColor blackColor];
        
        _titleLabel.backgroundColor = [UIColor clearColor];
        
        _titleLabel.numberOfLines = 0;
        
        _titleLabel.font = [UIFont systemFontOfSize:kTitleLabelFontSize];
        
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)leftImageView
{
    if (!_leftImageView)
    {
        _leftImageView = [[UIImageView alloc] init];
        
        _leftImageView.image = [[UIImage imageNamed:@"icon_bg_panel_left"] stretchableImageWithLeftCapWidth:0 topCapHeight:16];
        
        _leftImageView.userInteractionEnabled = YES;
        
        [self addSubview:_leftImageView];
        
        [self sendSubviewToBack:_leftImageView];
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView
{
    if (!_rightImageView)
    {
        _rightImageView = [[UIImageView alloc] init];
        
        _rightImageView.image = [[UIImage imageNamed:@"icon_bg_panel_right"] stretchableImageWithLeftCapWidth:16 topCapHeight:0];
        
        _rightImageView.userInteractionEnabled = YES;
        
        [self addSubview:_rightImageView];
        
        [self sendSubviewToBack:_rightImageView];
    }
    return _rightImageView;
}

-(UIImageView *)iconImageView
{
    if (!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] init];
        
        _iconImageView.image = [UIImage imageNamed:@"icon_next_normal"];
        
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [self addSubview:_iconImageView];
    }
    return _iconImageView;
}

#pragma mark - =====================UIView方法====================

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

#pragma mark - =====================创建UI====================
// 添加尖角图片
-(void)addConner:(CGRect)rect
{
    self.frame = rect;
    
    self.leftImageView.frame = CGRectMake(0, 0, rect.size.width/2, rect.size.height);
    
    self.rightImageView.frame = CGRectMake(rect.size.width/2, 0, rect.size.width/2, rect.size.height);
    
    //    self.iconImageView.frame = CGRectMake(rect.size.width-25, rect.size.height/2, 20, rect.size.height/2-13);
}

- (void)addNaviButton:(CGRect)rect
{
    self.naviButton.frame = rect;
}

- (UILabel *)paopaoWithTitle:(NSString *)title subtitle:(NSString *)subtitle
{
    CGFloat titleWidth = [self getSizeOfString:title maxWidth:10000.0 withFontSize:kTitleLabelFontSize].width;
    
    CGFloat subtitleWidth = [self getSizeOfString:subtitle maxWidth:10000.0 withFontSize:kTitleLabelFontSize].width;
    
    self.titleLabel.frame = CGRectZero;
    
    if (title&&subtitle)
    {
        self.titleLabel.text = [NSString stringWithFormat:@"%@\n%@",title,subtitle];
        
        [self.titleLabel sizeToFit];
        // 设置最小值
        if (titleWidth < kPaopaoTitleMinLength && subtitleWidth < kPaopaoTitleMinLength)
        {
            self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMinLength, self.titleLabel.frame.size.height);
        }// 限制最大值
        else if (titleWidth > kPaopaoTitleMaxLength || subtitleWidth > kPaopaoTitleMaxLength)
        {
            self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMaxLength, 0);
            
            [self.titleLabel sizeToFit];
        }
    }
    else
    {
        if (title)
        {
            self.titleLabel.text = title;
            
            [self.titleLabel sizeToFit];
            
            if (titleWidth < kPaopaoTitleMinLength)
            {
                self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMinLength, self.titleLabel.frame.size.height);
            }
            else if (titleWidth > kPaopaoTitleMaxLength)
            {
                self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMaxLength, 0);
                
                [self.titleLabel sizeToFit];
            }
        }
        else if (subtitle)
        {
            self.titleLabel.text = subtitle;
            
            [self.titleLabel sizeToFit];
            
            if (subtitleWidth < kPaopaoTitleMinLength)
            {
                self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMinLength, self.titleLabel.frame.size.height);
            }
            else if (subtitleWidth > kPaopaoTitleMaxLength)
            {
                self.titleLabel.frame = CGRectMake(0, 0, kPaopaoTitleMaxLength, 0);
                
                [self.titleLabel sizeToFit];
            }
        }
    }
    return self.titleLabel;
}

#pragma mark - =====================按键点击事件====================
- (void)btnAction
{
    if (self.sendNaviRequest)
    {
        self.sendNaviRequest( );
    }
}

// 获取指定最大宽度和字体大小的string的size
- (CGSize)getSizeOfString:(NSString *)string maxWidth:(float)width withFontSize:(int)fontSize
{
    //    UIFont *font = [UIFont systemFontOfSize:fontSize];
    //    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, 10000.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    return size;
}


@end
